package com.dayaokeji.rhythmschoolstudent.client.home.join

import android.os.Bundle
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.dayaokeji.rhythmschoolstudent.R
import com.dayaokeji.rhythmschoolstudent.client.common.ListFragment
import com.dayaokeji.rhythmschoolstudent.client.common.MultiListFragment
import com.dayaokeji.rhythmschoolstudent.client.home.adapter.JoinCourseListAdapter
import com.dayaokeji.rhythmschoolstudent.event.ReqNewCourseEvent
import com.dayaokeji.rhythmschoolstudent.event.UpdateCourseEvent
import com.dayaokeji.rhythmschoolstudent.utils.ServerCallback
import com.dayaokeji.rhythmschoolstudent.utils.ToastUtils
import com.dayaokeji.rhythmschoolstudent.utils.UserUtils
import com.dayaokeji.rhythmschoolstudent.wiget.ConfirmDialog
import com.dayaokeji.server_api.ApiUtils
import com.dayaokeji.server_api.ServerResponse
import com.dayaokeji.server_api.api.CourseApi
import com.dayaokeji.server_api.domain.AddCourse
import com.dayaokeji.server_api.domain.Course
import com.dayaokeji.server_api.domain.Meeting
import org.greenrobot.eventbus.EventBus
import retrofit2.Call

/**
 *********************************************************************
 **  Created by Zeno on 2018/7/13
 *********************************************************************
 **  version 1.0
 *********************************************************************
 **  Copyright (C) dayaokeji.com 2018 -- 2020 All rights reserved.
 *********************************************************************
 **
 *********************************************************************
 */
class JoinCourseFragment : MultiListFragment<Course, BaseViewHolder>(), Search {

    private val joinCourseListAdapter: JoinCourseListAdapter by lazy { JoinCourseListAdapter() }
    private val courseApi: CourseApi by lazy { ApiUtils.getApi(CourseApi::class.java) }

    private var key: String? = null
    private var addCourseCall: Call<ServerResponse<Void>>? = null

    companion object {
        fun newInstance(): JoinCourseFragment {
            return JoinCourseFragment()
        }
    }

    override fun search(key: String) {
        this.key = key
        onRefresh()
    }

    override fun createCall2(offset: Int, max: Int): Call<*>? {
        if (key == null) {
            return null
        }
        return ApiUtils.getApi(CourseApi::class.java).queryId(
                UserUtils.getUserInfo()!!.universityId,
                1,
                1,
                key,
                offset,
                max
        )
    }

    override fun createCall(offset: Int, max: Int): Call<*>? {
        if (key == null) {
            return null
        }
        return ApiUtils.getApi(CourseApi::class.java).queryId(
                UserUtils.getUserInfo()!!.universityId,
                1,
                2,
                key,
                offset,
                max
        )
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        adapter = joinCourseListAdapter

        joinCourseListAdapter.onItemChildClickListener = BaseQuickAdapter.OnItemChildClickListener { baseQuickAdapter: BaseQuickAdapter<Any, BaseViewHolder>, view: View, i: Int ->
            val size = baseQuickAdapter.data.size
            if (i < size) {
                val course = baseQuickAdapter.data[i] as Course
                when (view.id) {
                    R.id.tv_join_course -> {
                        val confirmDialog = ConfirmDialog.newInstance(ConfirmDialog.ConfirmInfo(content = "是否加入<b><font color='#0074FE'> ${course.name}</font> </b> 课程？"))
                        confirmDialog.show(childFragmentManager, "add_course")
                        confirmDialog.setConfirmClickListener(object : ConfirmDialog.ConfirmClickListener {
                            override fun click() {
                                addCourse(course.id)
                            }
                        })
                    }
                }
            }
        }
    }

    /**
     * 加入课程
     */
    private fun addCourse(courseId: Int) {
        val addCourse = AddCourse()
        addCourse.id = courseId.toString()
        addCourse.studentId = UserUtils.getUserInfo()?.id

        activity?.let {
            addCourseCall = courseApi.addMember(addCourse)
            addCourseCall?.enqueue(object : ServerCallback<Void>(it,"正在加入课程...") {
                override fun onResponse(success: Boolean, response: ServerResponse<Void>?) {
                    if (success) {
                        ToastUtils.success("加入课程成功")
                        EventBus.getDefault().post(UpdateCourseEvent())
                        EventBus.getDefault().post(ReqNewCourseEvent())
                    }
                }
            })
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        addCourseCall?.cancel()
    }
}